package com.ndp.fb.rdb.db.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.ndp.fb.rdb.api.EventNotificationMessageRdbService;
import com.ndp.fb.rdb.db.DaoTemplate;
import com.ndp.fb.rdb.db.PstAssign;
import com.ndp.fb.rdb.db.impl.mapper.DefaultRowMapper;
import com.ndp.fb.rdb.model.Pager;
import com.ndp.fb.rdb.model.message.EventNotificationMessage;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by elvisxiong on 7/23/15.
 */

@Service
public class EventNotificationMessageRdbServiceImpl extends DaoTemplate implements EventNotificationMessageRdbService {

    @Override
    public Long save(final EventNotificationMessage message) {
//        return saveResultId(message);
        String sql = "insert into event_notification_message(body,receiver,sender,send_time,event_type,status,expire_time,is_read,is_need_function_jump," +
                "url,event_no,channel,event_key,event_values) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        PstAssign pst = new PstAssign() {
            @Override
            public void setParam(PreparedStatement ps) throws SQLException {
                ps.setObject(1, message.getBody());
                ps.setObject(2, message.getReceiver());
                ps.setObject(3, message.getSender());
                ps.setObject(4, message.getSendTime());
                ps.setObject(5, message.getEventType().getType()); //枚举类型
                ps.setObject(6, message.getStatus().getStatus());
                ps.setObject(7, message.getExpiredTime());
                ps.setObject(8, message.getIsRead());
                ps.setObject(9,message.isNeedFunctionJump());
                ps.setObject(10,message.getUrl());
                ps.setObject(11,message.getEventNo());
                ps.setObject(12,message.getChannel().getType());
                ps.setObject(13,message.getEventKey());
                ps.setObject(14,message.getEventValues());
            }
        };
        return saveResultId(sql,pst);
    }

    @Override
    public Long updateReadStatusById(String id) {
        String sql = "update event_notification_message set is_read = 1 where id =?";
        int res =  getJdbcTemplate().update(sql,new Object[]{id});
        return new Long(res);
    }



    @Override
    public void delete(Long messageId) {
        String sql = "delete from event_notification_message where id = ?";
        getJdbcTemplate().update(sql, new Object[]{messageId});
    }


    @Override
    public Pager<EventNotificationMessage> findUnReadEventNotificationMessageByUserId(String userId, Integer pageNow, Integer pageSize) {
        Pager<EventNotificationMessage> pager = new Pager<>();
        List<EventNotificationMessage> eventNotificationMessageList = new ArrayList<>();
        String countSql = "select count(1) cnt from event_notification_message enm where enm.receiver = ? and enm.status = 1 and enm.is_read = 0 and enm.channel = 0";
        String pagerSql = "select * from event_notification_message enm where enm.receiver = ? and  enm.status = 1 and enm.is_read = 0 and enm.channel = 0 order by enm.send_time desc LIMIT ?,?";
        Object count = getJdbcTemplate().queryForMap(countSql.toString(), new Object[]{userId}).get("cnt");
        logger.debug("总数 >>>> " + count.toString());
        pager.setTotalSize(Long.parseLong(count.toString()));
        eventNotificationMessageList = getJdbcTemplate().query(pagerSql, new Object[]{userId, pageNow, pageSize}, DefaultRowMapper.autoRowMapper(EventNotificationMessage.class));
        pager.setResultList(eventNotificationMessageList);
        return pager;
    }


    @Override
    public EventNotificationMessage findUnReadEventNotificationMessageById(String id) {
        String sql = "select * from event_notification_message where id = ? and is_read = 0 and status = 1";
        return findOne(sql,new Object[]{id},DefaultRowMapper.autoRowMapper(EventNotificationMessage.class));
    }
}
